Page({
  data: {
    tableNo: null,
    launchOptions: null,
    showOptions: null
  },

  onLoad: function (options) {
    console.log('测试页面 onLoad 参数:', options)
    
    // 获取全局桌台号
    const app = getApp()
    const tableNo = app.getTableNo()
    
    this.setData({ 
      tableNo,
      launchOptions: options
    })
    
    // 显示启动参数
    wx.showModal({
      title: '页面启动参数',
      content: JSON.stringify(options),
      showCancel: false
    })
  },
  
  onShow: function () {
    console.log('测试页面 onShow')
    
    // 获取全局桌台号
    const app = getApp()
    const tableNo = app.getTableNo()
    
    // 获取当前页面显示参数
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    const showOptions = currentPage.options
    
    this.setData({ 
      tableNo,
      showOptions
    })
  },
  
  // 清除桌台号
  clearTableNo: function() {
    const app = getApp()
    app.clearTableNo()
    
    this.setData({
      tableNo: null
    })
    
    wx.showToast({
      title: '桌台号已清除',
      icon: 'success'
    })
  },
  
//生成二维码
    creatQrcode: function() {
      const APPID = '';
      const SECRET = '';
      // 获取access_token
      const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${SECRET}`;

      wx.request({
        url: url,
        method: "POST",
        success: function(res){
          const access_token = res.access_token;
          const qrcodeUrl = `https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=${access_token}`;
          const payload = {
                  path: 'pages/index/index',
                  query: 'tableNo=1',
                  scence: 1048,
                  width: 430
          };
          wx.request({
            url: qrcodeUrl,
            method: "POST",
            data: JSON.stringify(payload),
            success: function(data){
              console.log(data)
            }
          })
        }
      })
    //   fetch(url).then(res => res.json()).then(data => {
    //     const access_token = data.access_token;
    //     // 生成二维码
    //     const qrcodeUrl = `https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=${access_token}`;
    //     const payload = {
    //       path: 'pages/index/index',
    //       query: 'tableNo=1',
    //       scence: 1048,
    //       width: 430
    //     };
    //     fetch(qrcodeUrl, { method: 'POST', body: JSON.stringify(payload) })
    //       .then(res => res.json())
    //       .then(data => {
    //         // 处理返回的二维码图片URL
    //         console.log(data)
    //     });
    // });


      var tableNo = this.data.tableNo
     
    },

  // 设置测试桌台号
  setTestTableNo: function() {
    const app = getApp()
    const testTableNo = 'T99'
    app.setTableNo(testTableNo)
    
    this.setData({
      tableNo: testTableNo
    })
    
    wx.showToast({
      title: '已设置测试桌台号',
      icon: 'success'
    })
  },
  
  // 返回首页
  goToIndex: function() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  }
}) 